<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>我的</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<!--标准mui.css-->
		<link rel="stylesheet" href="css/mui.css">
		<!--App自定义的css-->

		<style>

		</style>
	</head>

	<body>

		<img src="" class="haedphoto" style="width: 200px;display: block;margin: 0 auto;" />
		<h2 class="username" style="font-size: 1em;text-align: center;width: 100%;"></h2>

		<p class="codep"></p>

	</body>
	<script src="../js/mui.min.js"></script>
	<script>
		mui.init({
			swipeBack: true //启用右滑关闭功能
		});
	</script>

	<script type="text/javascript" Author="Cchua">
		window.addEventListener('load', function() {
			var ua = navigator.userAgent.toLowerCase();
			if(ua.match(/MicroMessenger/i) != "micromessenger") {
				console.log("您使用的是非微信端的浏览器")
			} else {
				if(localStorage.userToken == undefined) {
					WechatNewLogin();
				} else {
					//					WechatLogin();
				}
			}

		});

		function WechatLogin() {
			document.querySelector(".codep").innerHTML = localStorage.userToken;
			mui.ajax({
				"url": URL_all+"api/wechat/openidata",
				"dataType": "json",
				"type": "post",
				"timeout": 100000000000,
				"headers": "{'Content-Type':'application/json','userToken':" + localStorage.userToken + "}",
				"success": function(data) {
					//					localStorage.userToken = data.dataResultObj.userToken
				},
				"error": function(xhr, type, errorThrown) {
					//异常处理；
					console.log(type);
				}
			})
		}

		function GetQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if(r != null) return unescape(r[2]);
			return null;
		}

		function WechatNewLogin() {
			var appid;
			var myurl = GetQueryString("code"); //001U6O851IsOzM1jQI851lNR851U6O8Y

			if(myurl != null && myurl.toString().length > 1) {
				var usercode = GetQueryString("code");
				mui.ajax({
					"url": URL_all+"api/wechat/postwcode",
					"dataType": "json",
					"type": "post",
					"data": "{'code':'" + usercode + "'}",
					"timeout": 10000,
					headers: {
						'Content-Type': 'application/json'
					},
					"success": function(data) {
						localStorage.userToken = data.dataResultObj.userToken
					},
					"error": function(xhr, type, errorThrown) {
						//异常处理；
						console.log(type);
					}
				})
			} else {
				var tourl = window.location.href;
				mui.ajax({
					"url": URL_all+"api/wechat/authorize",
					"dataType": "json",
					"type": "post",
					"timeout": 10000,
					headers: {
						'Content-Type': 'application/json'
					},
					"success": function(data) {
						appid = data.dataResultObj.AppId;
						localStorage.appId = data.dataResultObj.AppId;
						window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + tourl + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
					},
					"error": function(xhr, type, errorThrown) {
						//异常处理；
						console.log(type);
					}
				})
			}
		}
	</script>

</html>